package com.sqpoll.android.member;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

/**
 * The adapter for member list.
 * 
 * @author Younggue Bae
 */
public class MemberListAdapter extends BaseAdapter {

	Context mContext;
	LayoutInflater mInflater;
	int mLayoutResourceId;
	private List<Member> mRowList;

	public MemberListAdapter(Context context, int layoutResourceId) {
		this(context, layoutResourceId, new ArrayList<Member>());
	}
	
	public MemberListAdapter(Context context, int layoutResourceId, List<Member> items) {
		this.mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		this.mContext = context;
		this.mLayoutResourceId = layoutResourceId;
		this.mRowList = items;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		MemberListItemView itemView;
		if (convertView == null) {
			itemView = new MemberListItemView(mContext, mLayoutResourceId, (Member) getItem(position));
		} else {
			itemView = (MemberListItemView) convertView;
		}

		return itemView;
	}
	
	@Override
	public int getCount() {
		return mRowList.size();
	}

	@Override
	public Object getItem(int position) {
		return mRowList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
	public void addItem(Member item) {
		mRowList.add(item);
	}
	
	public void addItems(List<Member> items) {
		mRowList.addAll(items);
	}

}